package com.example.alladapter.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.alladapter.Adapter.RecycleViewAdapter;
import com.example.alladapter.Datas.PicName;
import com.example.alladapter.Fragment.Fragment1;
import com.example.alladapter.Fragment.Fragment2;
import com.example.alladapter.Fragment.Fragment3;
import com.example.alladapter.Fragment.Fragment4;
import com.example.alladapter.R;
import com.example.alladapter.utils.changebackground;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    /**
     * drownRecycleMenu 菜单
     */
    private RecyclerView recyclerView, drownRecycleMenu;
    private Button drawLayoutOpen;//按钮
    private TextView title;
    private CoordinatorLayout leftLayout;
    private ImageView imageView;
    private DrawerLayout drawerLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> viewPagers;
    private Fragment f1, f2, f3, f4;
    private SharedPreferences sp;
    private int fragmentId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences("Fragmrnt", MODE_PRIVATE);
        fragmentId = sp.getInt("fragment", 0);
        initView();
        Datas();
        Operation();
        ViewAdaptation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewPager.setCurrentItem(fragmentId);
        switch (fragmentId) {
            case 0:
                title.setText("文档浏览");
                break;
            case 1:
                title.setText("我的信息");
                break;
            case 2:
                title.setText("图片浏览");
                break;
            case 3:
                title.setText("图片处理相关");
                break;
        }
        sp.edit().putInt("fragment", 0).apply();

    }

    private void Datas() {
        viewPagers = new ArrayList<>();
        viewPagers.clear();
        f1 = new Fragment1(this);
        f2 = new Fragment2(this);
        f3 = new Fragment3(this);
        f4 = new Fragment4(this, viewPager);
        Fragment[] fragments = {f2, f1, f3, f4};
        viewPagers.addAll(Arrays.asList(fragments));
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), 0) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return viewPagers.get(position);
            }

            @Override
            public int getCount() {
                return viewPagers.size();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        title.setText("我的信息");
                        break;
                    case 1:
                        title.setText("文档信息");
                        break;
                    case 2:
                        title.setText("图片浏览");
                        break;
                    case 3:
                        title.setText("环球轨迹");
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void ViewAdaptation() {
        ArrayList<PicName> arrayListMenu = new ArrayList<>();
        String[] strings = {"我的信息", "文档信息", "图片浏览", "环球轨迹"};
        int[] ints = {0, 1, 2, 3};
        arrayListMenu.clear();
        for (int k = 0; k < strings.length; k++) {
            PicName picName = new PicName(ints[k], strings[k]);
            arrayListMenu.add(picName);
        }

        drownRecycleMenu.setLayoutManager(new LinearLayoutManager(this));
        RecycleViewAdapter recycleViewAdapter1 = new RecycleViewAdapter(0, MainActivity.this, new RecycleViewAdapter.OnRecycleClickListen() {
            @Override
            public void Onclick(int pos) {
                switch (pos) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        drawerLayout.closeDrawer(leftLayout);
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        drawerLayout.closeDrawer(leftLayout);
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        drawerLayout.closeDrawer(leftLayout);
                        break;
                    case 3:
                        viewPager.setCurrentItem(3);
                        drawerLayout.closeDrawer(leftLayout);
                        break;
                }
            }
        }, arrayListMenu);
        drownRecycleMenu.setAdapter(recycleViewAdapter1);

    }

    private void Operation() {
        myOnclick myOnclick = new myOnclick();
        drawLayoutOpen.setOnClickListener(myOnclick);
        imageView.setOnClickListener(myOnclick);

    }

    private void initView() {
        recyclerView = findViewById(R.id.rec);
        drawLayoutOpen = findViewById(R.id.drawLayoutOpen);
        drownRecycleMenu = findViewById(R.id.recycle_menu);
        leftLayout = findViewById(R.id.left_menu);
        drawerLayout = findViewById(R.id.drawerLayout);
        viewPager = findViewById(R.id.pager);
        imageView = findViewById(R.id.img);
        title = findViewById(R.id.tv_title);
    }

    private class myOnclick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.drawLayoutOpen:
                    new changebackground(imageView).startChange();
                    drawerLayout.openDrawer(leftLayout);
                    break;
                case R.id.img:
                    break;
            }
        }
    }
}